package com.ningmengs789.live.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import com.ningmengs789.live.R;
import com.ningmengs789.live.live.live.list.H_2_Fragment;
import com.ningmengs789.live.live.live.push.camera.TCLivePublisherActivity;
import com.ningmengs789.live.utils.UserInfoUtil;

import butterknife.BindView;
import butterknife.ButterKnife;

public class VideoLiveActivity extends BaseActivity {

    @BindView(R.id.tv_title_center)
    TextView tv_title_center;

    @BindView(R.id.btn_start_live)
    TextView btn_start_live;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_live);

        ButterKnife.bind(this);
        tv_title_center.setVisibility(View.VISIBLE);
        tv_title_center.setText("直播");

        H_2_Fragment fragment = new H_2_Fragment();
        Bundle bundle = new Bundle();
        bundle.putBoolean("ISHB", false);
        fragment.setArguments(bundle);
        addFragment(R.id.video_live_fl, fragment, "H_2_Fragment");

        btn_start_live.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isLogin()){
                    return;
                }
                //视频直播
                startActivity(new Intent(mContext, TCLivePublisherActivity.class));
                overridePendingTransition(R.anim.slide_right_entry, R.anim.hold);
            }
        });
    }

    private int info_complete;

    private boolean isLogin(){
        boolean login;
        if (info_complete == 0) {
            startActivity(new Intent(mContext, LoginActivity.class));
            login = false;
        } else {
            login = true;
        }
        return login;
    }

    //添加 Fragment
    protected void addFragment(int containerViewId, Fragment fragment, String tag) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        // 设置tag，不然下面 findFragmentByTag(tag)找不到
        fragmentTransaction.add(containerViewId, fragment, tag);
        fragmentTransaction.addToBackStack(tag);
        fragmentTransaction.commit();
    }

    public void getFinish(){
        finish();
    }

    @Override
    protected void onResume() {
        super.onResume();
        info_complete = UserInfoUtil.getInfoComplete();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}